package com.lin.spzx.manager.controller;

import com.lin.spzx.manager.service.ValidateCodeService;
import com.lin.spzx.model.dto.system.LoginDto;
import com.lin.spzx.model.entity.system.SysUser;
import com.lin.spzx.model.vo.common.Result;
import com.lin.spzx.model.vo.common.ResultCodeEnum;
import com.lin.spzx.model.vo.system.LoginVo;

import com.lin.spzx.manager.service.SysUserService;
import com.lin.spzx.model.vo.system.SysMenuVo;
import com.lin.spzx.model.vo.system.ValidateCodeVo;

import com.lin.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户接口")
@RestController//返回json格式
@RequestMapping(value = "/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService ;


    @Operation(summary = "登录的方法")
    @PostMapping(value = "/login")
    //1
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto) ;
        return Result.build(loginVo , ResultCodeEnum.SUCCESS) ;
    }

    @Autowired
    private ValidateCodeService validateCodeService;

    //2.生成图片验证码，核心部分在service层里
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        //返回一个包含验证码的Vo对象
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo,ResultCodeEnum.SUCCESS);
    }

    //3.获取用户信息，拦截器后优化
    @GetMapping(value = "/getUserInfo")
    public Result getUserInfo(@RequestHeader(name = "token") String token) {
        //1 从请求头获取token
        //String token = request.getHeader("token");
        //2 根据token查询redis获取用户信息
        SysUser sysUser = sysUserService.getUserInfo(token);

        //3 用户信息返回
        return Result.build(sysUser,ResultCodeEnum.SUCCESS);
    }
    //第二个优化获取用户信息
  /*  @GetMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo() {
        return Result.build(AuthContextUtil.get()  ,
                ResultCodeEnum.SUCCESS) ;
    }*/

    //4.用户退出
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }






    //用户退出
  /*  @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }*/


    //获取用户信息
  /*  @GetMapping(value = "/getUserInfo")
    public Result getUserInfo(@RequestHeader(name = "token") String token) {
        //1 从请求头获取token
//        String token = request.getHeader("token");
        //2 根据token查询redis获取用户信息
        SysUser sysUser = sysUserService.getUserInfo(token);

        //3 用户信息返回
        return Result.build(sysUser,ResultCodeEnum.SUCCESS);
    }
*/
    //第二个优化获取用户信息
  /*  @GetMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo() {
        return Result.build(AuthContextUtil.get()  ,
                ResultCodeEnum.SUCCESS) ;
    }*/


    //生成验证码
    //生成图片验证码
  /*  @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo,ResultCodeEnum.SUCCESS);
    }



    @Operation(summary = "查询用户可以操纵的菜单")
    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findUserMenuList() ;
        return Result.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }*/




}